//
//  SWJoinableGroupView.swift
//  MiU
//  可加入的圈子
//  Created by miss on 15/12/10.
//  Copyright © 2015年 MiU. All rights reserved.
//

import Foundation
import UIKit

private let CellTopMargin:CGFloat = 0
private let CellBottomMargin:CGFloat = 0

class SWGroupView: UIView,ClickActionDelegate
{
    weak var delegate:ClickActionDelegate?
    
    lazy private var _noGroupTip:UILabel = UILabel()
    
    private var _collection:SWGroupCollection?
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setup()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var data:[Any]?
    {
        didSet
        {
            if let data = data, data.count > 0
            {
                _noGroupTip.isHidden = true
                _collection?.isHidden = false
                _collection?.data = data
            }
            else
            {
                _noGroupTip.isHidden = false
                _collection?.isHidden = true
            }
            
        }
    }
    
    class func getCellWidth(containerWidth:CGFloat) -> CGFloat
    {
        //一个屏幕内显示2.2个
        let CellWidth:CGFloat = containerWidth / 2.2
        return CellWidth
    }
    
    class func getHeight(containerWidth:CGFloat) -> CGFloat
    {
        let CellHeight:CGFloat = getCellWidth(containerWidth: containerWidth)
        let height:CGFloat = CellTopMargin + CellHeight + CellBottomMargin
        return height
    }
    
    private func setup()
    {
        let TopMargin:CGFloat = 14
        
        backgroundColor = UIColor.white
        
        let PageWidth:CGFloat = SWGroupView.getCellWidth(containerWidth: width)
        let PageHeight:CGFloat = PageWidth
        _collection = SWGroupCollection(frame: CGRectMake(0, CellTopMargin, width, PageHeight), originWidth: PageWidth, grid: 3)
        _collection?.delegate = self
        addSubview(_collection!)
        
        _noGroupTip = UILabel.labelWithLimitWidth(width - 20, text: "没有照片", font: UIFont.systemFont(ofSize: 16))
        _noGroupTip.textColor = UIColor.lightGray
        _noGroupTip.textAlignment = .center
        _noGroupTip.frame = CGRectMake(10, 100, _noGroupTip.width, _noGroupTip.height)
        _noGroupTip.isHidden = true
        addSubview(_noGroupTip)
        
        
    }
    
    func view(_ view:UIView, click action:String, argument aArgument:[AnyHashable:Any]?)
    {
        if action == CallbackAction.Tap
        {
            if let index = aArgument?["index"] as? Int
            {
                delegate?.view(self, click: CallbackAction.JoinGroup, argument: ["index":index])
            }
        }
    }
    
    
    
    
    
}
